package org.example;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
//Student类
public class Map_Demo3 {
    public static void main(String[] args) {
        //HashMap键位置如果存的是自定义对象，需要重写HashCode和equals方法

        Map<Student,String> map=new HashMap<Student,String>();
        Student s1=new Student("张三",18);
        Student s2=new Student("李四",19);
        Student s3=new Student("王五",20);
        Student s4=new Student("王五",20);
        map.put(s1,"广州");
        map.put(s2,"河南");
        map.put(s3,"南京");
        map.put(s4,"湛江");

        //根据键找值遍历
        Set<Student> k1 = map.keySet();
        for (Student key : k1) {
            //map.get(key)是的到键的值
            String value = map.get(key);
            System.out.println(key+"="+value);
        }
        System.out.println("----------------1");
        //根据键值对遍历
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key=entry.getKey();
            String value=entry.getValue();
            System.out.println(key+"="+value);
        }
        System.out.println("----------------2");
        //根据lambda表达式遍历
        map.forEach((k,v)-> System.out.println(k+"="+v));
        System.out.println("----------------3");
        //根据匿名内部类遍历
        map.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student k, String v) {
                System.out.println(k+"="+v);
            }
        });
        System.out.println("----------------4");


        System.out.println("=============");
        //LinkHashMap 底层数据结构是哈希表
        // 有序(存和取顺序一样) 不重复
        //双链表机制记录存储的顺序
        LinkedHashMap<Student,String> lHM = new LinkedHashMap<>();
        lHM.put(s1,"广州");
        lHM.put(s2,"河南");
        lHM.put(s3,"南京");
        lHM.put(s4,"湛江");
        System.out.println(lHM);











    }
}
